/*********************·*************************
 * Copyright (c) 2022
 * All rights reserved.
 *
 * 文件名称：mqtt_conf.c
 * 摘 要：解析配置文件
 *
 * 当前版本：1.0
 * 作 者：任慧
 * 完成日期：2022年8月1日
 *
 * ********************************************/

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "iniparser.h"
#include "dictionary.h"
#include "mqtt_conf.h"
#include "platform.h"
//#include "zlog.h"

int get_mqtt_conf(mqtt_t *mqtt,int platform)
{
	dictionary      *ini=NULL;
	const char      *client_id = NULL;
	const char      *host = NULL;
	const char      *username = NULL;
	const char      *password = NULL;
	const char      *topic = NULL;
	const char      *version = NULL;
	const char      *identifer = NULL;
	int              port;
	int              Qos;
	int              alive;
	int              interval_time;
	const char      *method = NULL;
	const char      *jsonid = NULL;
	const char      *zlog_path = NULL;

	struct hostent  *get_host = NULL;
	char            *get_ip = NULL;


	if (platform < 0)
	{
		return -1;
	}

	if (platform == ALIYUN)
	{

		ini=iniparser_load("./etc/aliyun.conf");
		if (ini == NULL)
		{
			printf("inipar_load  failure\n");
			return -2;
		}


		client_id = iniparser_getstring(ini, "client_id:id", "null");
		host = iniparser_getstring(ini, "mqtt_server_addr:host", "null");
		username = iniparser_getstring(ini, "user_passwd:username", "null");
		password = iniparser_getstring(ini, "user_passwd:passwd", "null");
		version = iniparser_getstring(ini, "ali_json:version", "null");
		jsonid = iniparser_getstring(ini, "ali_json:id", "1502446751");
		identifer = iniparser_getstring(ini, "ali_json:identifer", "CurrentTemperature");
		port = iniparser_getint(ini, "mqtt_server_addr:port", DEFAULT_PORT);
		Qos = iniparser_getint(ini, "ali_Qos:Qos", DEFAULT_QOS);
		alive = iniparser_getint(ini, "KEEP_ALIVE:alive", DEFAULT_ALIVE);
		interval_time = iniparser_getint(ini, "interval_time:time", 10);

		topic = iniparser_getstring(ini, "pub_topic:topic", "null");
		method = iniparser_getstring(ini, "ali_json:method", "null");
		zlog_path = iniparser_getstring(ini, "zlog_path:path", "null");



		strncpy(mqtt->client_id, client_id, MAXSIZE);
		strncpy(mqtt->host, host, MAXSIZE);
		strncpy(mqtt->username, username, MAXSIZE);
		strncpy(mqtt->password, password, MAXSIZE);
		strncpy(mqtt->version, version, MAXSIZE);
		strncpy(mqtt->jsonid, jsonid, MAXSIZE);
		strncpy(mqtt->identifer, identifer, MAXSIZE);
		strncpy(mqtt->topic, topic, MAXSIZE);
		strncpy(mqtt->method, method, MAXSIZE);
		strncpy(mqtt->zlog_path, zlog_path, MAXSIZE);

		mqtt->platform = platform;
		mqtt->Qos = Qos;
		mqtt->port = port;
		mqtt->alive = alive;
		mqtt->interval_time = interval_time;
	}

	if (platform == HUAWEIYUN)
	{
		ini=iniparser_load("./etc/huaweiyun.conf");
		if (ini ==NULL)
		{
			printf("inipar_load  failure\n");
			return -2;
		}

		client_id = iniparser_getstring(ini, "client_id:id", "null");
		host = iniparser_getstring(ini, "mqtt_server_addr:host", "null");
		username = iniparser_getstring(ini, "user_passwd:username", "null");
		password = iniparser_getstring(ini, "user_passwd:passwd", "null");
		jsonid = iniparser_getstring(ini, "ali_json:id", "null");
		identifer = iniparser_getstring(ini, "ali_json:identifer", "null");
		port = iniparser_getint(ini, "mqtt_server_addr:port", DEFAULT_PORT);
		Qos = iniparser_getint(ini, "ali_Qos:Qos", DEFAULT_QOS);
		alive = iniparser_getint(ini, "KEEP_ALIVE:alive", DEFAULT_ALIVE);
		interval_time = iniparser_getint(ini, "interval_time:time", 10);
		topic = iniparser_getstring(ini, "pub_topic:topic", "null");
		zlog_path = iniparser_getstring(ini, "zlog_path:path", "null");


		get_host = gethostbyname(host);
		get_ip = inet_ntoa(*(struct in_addr *)get_host->h_addr);

		strncpy(mqtt->client_id, client_id, MAXSIZE);
		strncpy(mqtt->host, get_ip, MAXSIZE);
		strncpy(mqtt->username, username, MAXSIZE);
		strncpy(mqtt->password, password, MAXSIZE);
		strncpy(mqtt->jsonid, jsonid, MAXSIZE);
		strncpy(mqtt->identifer, identifer, MAXSIZE);
		strncpy(mqtt->topic, topic, MAXSIZE);
		strncpy(mqtt->zlog_path, zlog_path, MAXSIZE);

		mqtt->platform = platform;
		mqtt->Qos= Qos;
		mqtt->port = port;
		mqtt->alive = alive;
		mqtt->interval_time = interval_time;

	}

	if (platform == TENGXUNYUN)
	{

		ini=iniparser_load("./etc/tengxunyun.conf");
		if (ini ==NULL)
		{
			printf("inipar_load  failure\n");
			return -2;
		}

		client_id = iniparser_getstring(ini, "client_id:id", "null");
		host = iniparser_getstring(ini, "mqtt_server_addr:host", "null");
		username = iniparser_getstring(ini, "user_passwd:username", "null");
		password = iniparser_getstring(ini, "user_passwd:passwd", "null");
		jsonid = iniparser_getstring(ini, "ali_json:id", "123");
		identifer = iniparser_getstring(ini, "ali_json:identifer", "CurrentTemperature");
		port = iniparser_getint(ini, "mqtt_server_addr:port", DEFAULT_PORT);
		Qos = iniparser_getint(ini, "ali_Qos:Qos", DEFAULT_QOS);
		alive = iniparser_getint(ini, "KEEP_ALIVE:alive", DEFAULT_ALIVE);
		interval_time = iniparser_getint(ini, "interval_time:time", 10);

		topic = iniparser_getstring(ini, "pub_topic:topic", "null");
		method = iniparser_getstring(ini, "ali_json:method", "null");
		zlog_path = iniparser_getstring(ini, "zlog_path:path", "null");


		strncpy(mqtt->client_id, client_id, MAXSIZE);
		strncpy(mqtt->host, host, MAXSIZE);
		strncpy(mqtt->username, username, MAXSIZE);
		strncpy(mqtt->password, password, MAXSIZE);
		strncpy(mqtt->jsonid, jsonid, MAXSIZE);
		strncpy(mqtt->identifer, identifer, MAXSIZE);
		strncpy(mqtt->topic, topic, MAXSIZE);
		strncpy(mqtt->method, method, MAXSIZE);
		strncpy(mqtt->zlog_path, zlog_path, MAXSIZE);

		mqtt->platform = platform;
		mqtt->Qos = Qos;
		mqtt->port = port;
		mqtt->alive = alive;
		mqtt->interval_time = interval_time;
	}

	iniparser_freedict(ini);
	return 0;
}

